//
//  JXWWebViewController.m
//  JXWShopping4IPAD
//
//  Created by zheng ji on 14-3-9.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "JXWWebViewController.h"

@interface JXWWebViewController ()
{
    UIActivityIndicatorView* _activityIndicatorView;
    
    UIWebView* _webView;
    

}

@end

@implementation JXWWebViewController
 @synthesize backButton, forwardBarButtonItem, refreshBarButtonItem, stopBarButtonItem  ;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.view.backgroundColor=[UIColor whiteColor];
 
    self.navigationItem.title=@"本地游";
     [self updateToolbarItems];
    
   
    
    
    //CGRect webFrame=self.view.frame;
    _webView.backgroundColor=[UIColor whiteColor];
    
    _webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024, 708)];
              
    //_webView=[[UIWebView alloc]initWithFrame:CGRectMake(webFrame.origin.x, webFrame.origin.y, webFrame.size.width  , webFrame.size.height-subHeight)];
    
    _webView.opaque=NO;
    _webView.scalesPageToFit=YES;
    
    
    _webView.delegate=self;
    [self.view addSubview:_webView];
    
    _activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 32.0f, 32.0f)];
    [_activityIndicatorView setCenter:self.view.center];
    [_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    _activityIndicatorView.center=_webView.center;
	
    
    [self.view addSubview:_activityIndicatorView];
    
    [self loadWebUrl];

}
-(void)viewDidDisappear:(BOOL)animated{
    _webView.delegate=nil;
    _webView=nil;
    [super viewDidDisappear:animated];
}
- (void)goBackClicked:(UIBarButtonItem *)sender {
    [_webView goBack];
}

- (void)goForwardClicked:(UIBarButtonItem *)sender {
    [_webView goForward];
}

- (void)reloadClicked:(UIBarButtonItem *)sender {
    [_webView reload];
}

- (void)stopClicked:(UIBarButtonItem *)sender {
    [_webView stopLoading];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	[self updateToolbarItems];
}

- (void)closeButtonClicked:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIBarButtonItem *)backButton {
    
    if (!backButton) {
        backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackClicked:)];
        backButton.imageInsets = UIEdgeInsetsMake(2.0f, 0.0f, -2.0f, 0.0f);
		//backButton.width = 18.0f;
    }
    return backButton;
}

- (UIBarButtonItem *)forwardBarButtonItem {
    
    if (!forwardBarButtonItem) {
        forwardBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward"] style:UIBarButtonItemStylePlain target:self action:@selector(goForwardClicked:)];
        forwardBarButtonItem.imageInsets = UIEdgeInsetsMake(2.0f, 0.0f, -2.0f, 0.0f);
		//forwardBarButtonItem.width = 18.0f;
    }
    return forwardBarButtonItem;
}

- (UIBarButtonItem *)refreshBarButtonItem {
    
    if (!refreshBarButtonItem) {
        refreshBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadClicked:)];
    }
    
    return refreshBarButtonItem;
}



-(void)updateToolbarItems{
    
    
    UIBarButtonItem *refreshStopBarButtonItem = _webView.isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;
    
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width = 1.0f;
    
    
    NSArray *items=[NSArray arrayWithObjects:
                    fixedSpace,
                    self.backButton,
                    fixedSpace,
                    self.forwardBarButtonItem,
                    fixedSpace,
                    refreshStopBarButtonItem,
                    nil];
    
    self.navigationItem.rightBarButtonItems=items;
    
    UIBarButtonItem* closeButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonClicked:)];
    
    
    
    self.navigationItem.leftBarButtonItem=closeButtonItem;
}
-(void)loadWebUrl{
    //self.webURL=@"http://redirect.simba.taobao.com/rd?w=unionnojs&f=http%3A%2F%2Fre.taobao.com%2Feauction%3Fe%3DTo%252BbJIl1R8IjmraEDZVrLp61ICM83YB2sfSjPqrk7DyLltG5xFicOSZqewpHPyZzZi676KxXd4eTxvbrPnZQIop6bxIMvfVXj1VBkHsi6EKB3ujUJI0OeA%253D%253D%26ptype%3D100010&k=e2e107a2b72ca1b1&c=un&b=alimm_0&p=mm_17287198_5822714_20622370";
    if(self.webURL !=nil && _webView !=nil){
        
        
        
        [_webView loadRequest:[[NSURLRequest alloc]
                               initWithURL:[NSURL URLWithString:self.webURL]]];
        
    }
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
    UIApplication* uiApp=[UIApplication sharedApplication];
    uiApp.networkActivityIndicatorVisible=YES;
    [_activityIndicatorView startAnimating];
    //[self updateToolbarItems];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGSize contentSize = webView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;
    
    float scale = viewSize.width / contentSize.width;
    if (scale < 0.9) {
        NSLog(@"Zoom out fix for web view: %f", scale);
        
        webView.scrollView.minimumZoomScale = scale;
        webView.scrollView.maximumZoomScale = scale+5;
        webView.scrollView.zoomScale = scale;
    }
    
    
    [_activityIndicatorView stopAnimating];
    UIApplication* uiApp=[UIApplication sharedApplication];
    
    uiApp.networkActivityIndicatorVisible=NO;
    //[self updateToolbarItems];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    _webView.delegate=nil;
    _webView=nil;
}
@end
